package ext.acae2.object.helper;

import java.lang.reflect.Method;

import wt.doc.WTDocument;
import wt.doc.WTDocumentHelper;
import wt.fc.PersistenceHelper;
import wt.fc.PersistenceServerHelper;
import wt.org.WTPrincipalReference;
import wt.pom.UniquenessException;
import wt.session.SessionHelper;
import wt.util.WTException;
import wt.util.WTPropertyVetoException;
import wt.vc.Iterated;
import wt.vc._IterationInfo;
import ext.acae2.utilities.util.CSCUtil;

/**
 * @desription 重命名帮助类
 * @author cnie
 * @date 2013-7-25 下午12:55:32
 */
public class ObjectIdentifiedHelper {

	
	public static synchronized void changeWTDocumentIdentity(WTDocument doc,String newName,String newNumber){
		String currentUserName ="";
		try {
			WTPrincipalReference currentPrincipalRef = SessionHelper.manager.getPrincipalReference();
			currentUserName = SessionHelper.manager.getPrincipal().getName();
			SessionHelper.manager.setAdministrator();
			doc = WTDocumentHelper.service.changeWTDocumentIdentity(doc, newName,newNumber, doc.getOrganization());
			setModifier(doc,currentPrincipalRef);
			PersistenceServerHelper.manager.update(doc);
			doc=(WTDocument) PersistenceHelper.manager.refresh(doc);
		}  catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				SessionHelper.manager.setPrincipal(currentUserName);
			} catch (WTException e) {
				e.printStackTrace();
			}
		}
	}
	
/*	public static void test(String oid){
		
		String currentUserName ="";
		try{
			WTPrincipalReference currentPrincipalRef = SessionHelper.manager.getPrincipalReference();
			currentUserName = SessionHelper.manager.getPrincipal().getName();
			SessionHelper.manager.setAdministrator();
			WTDocument doc = (WTDocument) CSCUtil.getObjectByOid(oid);
			changeWTDocumentIdentity(doc,"ACAE_EPICCA_档案系统集成 1",null);
			setModifier(doc,currentPrincipalRef);
			PersistenceServerHelper.manager.update(doc);
			doc=(WTDocument) PersistenceHelper.manager.refresh(doc);
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				SessionHelper.manager.setPrincipal(currentUserName);
			} catch (WTException e) {
				e.printStackTrace();
			}
		}
	}*/
	
	  protected static void setModifier(Iterated obj, WTPrincipalReference uref) throws Exception {
			try {
				obj.getCreator();
				Class[] pp = new Class[] { WTPrincipalReference.class };
				Method setCreator = _IterationInfo.class.getDeclaredMethod("setModifier", pp);
				setCreator.setAccessible(true);
				setCreator.invoke(obj.getIterationInfo(), new Object[] { uref });
			} catch (Exception e) {
				// System.out.println(">>>Error:" + e.toString());
				e.printStackTrace();
				throw e;
			}

		}
	
	public static void main(String[] args) throws WTException {
		//"VR:wt.doc.WTDocument:304220"
	}

}
